﻿using System;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SZSLSystem.Common;
using SZSLSystem.Web.UI;

namespace SZSLSystem.Web.Admin.Role
{
    public partial class List : SZSLSystem.Web.UI.ManagePage
    {
        #region 全局变量

        /// <summary>
        /// 总条数
        /// </summary>
        public int pcount;    
        /// <summary>
        /// 当前页
        /// </summary>
        public int page;      
        /// <summary>
        /// 设置每页显示的大小
        /// </summary>
        public int pagesize;
        /// <summary>
        /// 组织机构编号
        /// </summary>
        public string orgid;
        /// <summary>
        /// 组织机构名称
        /// </summary>
        public string orgName;
        /// <summary>
        /// 组织机构类型
        /// </summary>
        public string orgType;

        public bool CheckUpdate;
        public bool CheckAdd;

        #endregion 

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.pagesize = webset.BackgroundPage;
            if (!Page.IsPostBack)
            {
                this.RptBind("", "Sort desc");
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="orderby">排序条件</param>
        private void RptBind(string strWhere, string orderby)
        {
            if (!int.TryParse(Request.Params["page"] as string, out this.page))
            {
                this.page = 0;
            }
            SZSLSystem.BLL.Role bll = new SZSLSystem.BLL.Role();
            this.pcount = bll.GetCount(strWhere);
            if (this.pcount > 0)
            {
                this.lbtnDel.Enabled = true;
            }
            else
            {
                this.lbtnDel.Enabled = false;
            }
            DataTable tb = bll.GetPageList(this.pagesize, this.page, strWhere, orderby).Tables[0];
            this.rptList1.DataSource = tb;
            this.rptList1.DataBind();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            StringBuilder str = new StringBuilder();
            string keyword = this.txtKeywords.Text.Trim();
            if (!string.IsNullOrEmpty(keyword))
            {
                str.Append(" 1=1 ");
                str.Append(" and ID='" + new SZSLSystem.BLL.Role().GetModel(keyword).ID + "' ");
                this.RptBind(str.ToString(), "Sort desc");
            }
            else
            {
                JscriptPrint("请输入角色名称", "", "Warn");
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            try
            {
                SZSLSystem.BLL.Role rolebll = new BLL.Role();
                for (int i = 0; i < this.rptList1.Items.Count; i++)
                {
                    Guid id = Guid.Parse(((Label)this.rptList1.Items[i].FindControl("lb_id")).Text.ToString());
                    string name = ((Label)this.rptList1.Items[i].FindControl("lb_name")).Text.ToString();
                    CheckBox cb = (CheckBox)this.rptList1.Items[i].FindControl("cb_id");
                    if (cb.Checked)
                    {
                        rolebll.Delete(id);
                        //
                        //保存日志
                        //
                        SaveLogs("[组织机构]删除组织机构角色成功！组织机构角色名称：" + name, 0);
                    }
                }
                JscriptPrint("删除组织机构角色成功啦！", "List.aspx?OrgID=" + Request.Params["OrgID"], "Success");
            }
            catch (Exception err)
            {
                //
                //异常日志写入
                //
                SaveLogs(err.ToString(), 1);

                JscriptMsg(350, 230, "错误提示", "<b>出现错误了！</b>删除组织机构角色失败啦！", "List.aspx?OrgID=" + Request.Params["OrgID"], "Error");
            }
        }
    }
}
